<?php
namespace XtwsBack\Controller;
use Think\Controller;

class AreaController extends CommonController {
    public function index(){
		$area=D('area');
		$getfirst=$area->where("`re_id` =0")->select();
		$this->assign('areali',$getfirst);
        $this->display();
    }//end fun
	
	//获取下一级所有地址
	public function getnext(){
		$area=D('area');
		$getall=$area->where("`re_id` =".$_GET['rid'])->select();
		$this->ajaxReturn($getall,'json');
	}//end fun
	
	//获取当前完整地址
	public function getinfo(){
		$area=D('area');
		$info=array();
		$info=$this->getre($info,$_GET['id']);
		$nowadd=array();
		foreach($info as $i=>$val){
			$nowadd[]=$val['name'];
		}
		//倒序排列
		$nowadd=array_reverse($nowadd);
		//转为字符串
		$nowadd=implode("-",$nowadd);
		$info['nowadd']=$nowadd;
		//dump($info);
		
		$this->ajaxReturn($info,'json');
	}//end fun
	
	//返回上级地区-回调函数
	public function getre($areaarr,$rid){
		$area=D('area');
		$getinfo=$area->where("id=".$rid)->find();
		$areaarr[]=$getinfo;
		if($getinfo["re_id"]==0){
			}else{
				$areaarr=$this->getre($areaarr,$getinfo["re_id"]);
			}
		return $areaarr;
	}//end fun
	
	public function savearea(){
		$area=D('area');
		if($_POST['id']==''){
			//新增
			    $newadd["area_id"] =$_POST['area_id'];
				$newadd["name"] =$_POST['name'];
				$newadd["re_id"] =$_POST['re_id'];
				$newadd["lev"] =$_POST['lev'];
				$newadd["disorder"] =$_POST['disorder'];
				$alt=$area->add($newadd);
				if($alt>0){
					$this->ajaxReturn("添加成功^_^");
					}else{
						$this->ajaxReturn("添加失败-_-");
					}
			}else{
				//更新
				$alt=$area->save($_POST);
				if($alt>0){
					$this->ajaxReturn("更新成功^_^");
					}else{
						$this->ajaxReturn("更新失败-_-");
					}
			}
	}//end fun
	
}//end class